<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>数字人面试官管理</span>
      </div>
      <div style="margin-left: 120px">
        <h3>数字人形象图</h3>
        <el-upload
            class="upload-demo"
            drag
            action="http://172.20.0.144:8680/upload/uploadToOss"
            :on-success="handleAvatarSuccess"
            multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </div>
      <el-form :model="form" :rules="rules" ref="form" label-width="120px" style="width: 700px">
        <el-form-item label="形象图链接" prop="image">
          <el-input v-model="form.image"></el-input>
        </el-form-item>
        <el-form-item label="面试官名称" prop="aiName">
          <el-input v-model="form.aiName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="resetForm">取消</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="数字人形象">
          <template slot-scope="scope">
            <img :src="scope.row.image" alt="Interviewer Image" style="max-width: 100px; max-height: 100px;"></img>
          </template>
        </el-table-column>
        <el-table-column prop="aiName" label="面试官名称"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updatedTime" label="更新时间"></el-table-column>

      </el-table>
      <div v-if="tableData.length === 0" style="text-align: center;">暂无数据</div>
    </el-card>
    <el-notification
        :title="notificationTitle"
        :message="notificationMessage"
        type="info"
        :duration="3000"
    ></el-notification>
  </div>
</template>

<script>

import http from '@/http.js';

export default {
  data() {
    return {
      imageUrl: null,
      form: {
        image: '',
        aiName: ''
      },

      pageForm: {
        pageSize: 10,
        pageNumber: 1
      },
      rules: {
        image: [
          {required: true, message: '请上传形象图或填入外部链接', trigger: 'blur'}
        ],
        aiName: [
          {required: true, message: '面试官的名称不能为空', trigger: 'blur'}
        ]
      },
      tableData: [],
      notificationTitle: '',
      notificationMessage: '',
      total: 0
    };
  },
  mounted() {
    // 在页面加载时，从后端获取数据并显示在表格中
    this.fetchData();
  },
  methods: {
    fetchData() {
      http.post('/interviewer/listInterviewer', this.pageForm).then(res => {
        this.tableData = res.records;
        this.total = res.total
      })

    },
    handleAvatarSuccess(res, file) {
      console.log(res);

      console.log(file);
      this.form.image = res.data;

    },
    handleAvatarChange(file, fileList) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      if (isJPG || isPNG && isLt2M) {
        const reader = new FileReader();
        reader.readAsDataURL(file.raw);
        reader.onload = (e) => {
          this.imageUrl = e.target.result;
        };
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG || isPNG && isLt2M;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 这里可以添加提交表单数据的逻辑，例如发送AJAX请求
          console.log('表单提交成功', this.form);
          http.post('/interviewer/addInterviewer', this.form).then(res => {
            this.fetchData();
          })

          this.resetForm();
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.imageUrl = null;
    }
  }
};
</script>

<style scoped>
.avatar-uploader.icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.box-card {
  margin-bottom: 20px;
}
</style>